"use strict";
cc._RF.push(module, '16337KV5lVPd7IHcUCHwLW2', 'BetControl');
// scripts/chatRoom/BetControl.js

"use strict";

/**
 * 聊天室信息管理中心
 */

cc.Class({
    extends: cc.Component,

    properties: {
        rtPeriods: cc.RichText, //距离截止
        labTime: cc.Label, //截止时间
        curRewardInfo: cc.Node, //当前记录  
        labBet: cc.Label, //幸运，加拿大28期数信息
        X5SSCReward: cc.Prefab, //11选五、时时彩开奖列表预制

        bet28Reward: cc.Prefab, //幸运、加拿大28预制
        isuseContent: cc.Layout, //当前历史列表
        labNullR: cc.Node, //当前列表节点

        rewardHistoryIssue: cc.Node,

        spHistoryArrow: cc.Sprite,

        isuseNum: 0, //当前期号
        beginTimeStamp: 0, //开始时间
        endTimeStamp: 0, //结束时间

        lotteryId: "", //彩种id
        isWaitOpen: false, //是否等待开奖中
        isUpdating: false, //刷新历史记录中
        isSynchrTime: true, //是否同步服务器时间
        scInterTime: 0, //服务器和客户端相差时间
        isPre: false, //是否预售

        openList: [], //开奖列表

        _hasCurisuse: false, //判断是否有当期信息
        _IsuseListData: [],
        _createNode: false, //是否创建开奖列表
        _rewardListType: null //历史开奖列表记录是否显示正常

    },

    // use this for initialization
    start: function start() {
        this._createNode = false;
        this._rewardListType = null;
        //初始化开奖信息栏
        var historyRewardDecNode = null;

        //当期信息
        this.getCurrentIsuse();
        this.getIsuseList(this.lotteryId, "1", "11");

        window.Notification.on('rOpen_push', function (data) {
            //  cc.log("chat rOpen_push");
            this.refreshOpen(data.data);
        }.bind(this));

        //每5s刷新一次历史期数
        this.schedule(function () {
            if (this.isWaitOpen) {
                if (!this._hasCurisuse) {
                    this.getCurrentIsuse();
                }

                this.isWaitOpen = false;

                if (this._rewardListType == 0) {
                    this.getIsuseList(this.lotteryId, "1", "1");
                } else {
                    this.getIsuseList(this.lotteryId, "1", "11");
                }
            }
        }, 10);
    },

    initBetControl: function initBetControl(lotteryId) {
        this.lotteryId = lotteryId.toString();
        this.isSynchrTime = true;
    },

    //倒计时显示刷新
    betUpdater: function betUpdater(dt) {
        var currentTimeStamp = CL.MAINMANAGE.getServerTime();
        var time = 0;
        var leftTimeStamp = this.endTimeStamp - currentTimeStamp;

        if (leftTimeStamp >= 0) {
            time = this.endTimeStamp - currentTimeStamp;
        } else {
            this.labTime.string = "--:--:--";
            this.unschedule(this.betUpdater);
            this.refreshOpen(this.openList);
            this.refreshShow();
            return;
        }

        var h = parseInt(time / (60 * 60)).toString();
        var hStr = (parseInt(h) % 24).toString();
        var dStr = parseInt(parseInt(h) / 24).toString();
        hStr = hStr.length == 1 ? "0" + hStr : hStr;
        var m = parseInt((time - h * 60 * 60) / 60).toString();
        m = m.length == 1 ? "0" + m : m;
        var s = parseInt(time - h * 60 * 60 - m * 60).toString();
        s = s.length == 1 ? "0" + s : s;
        var timeStr = "";
        if (parseInt(dStr) > 0) {
            timeStr = dStr + "天 " + hStr + ":" + m + ":" + s;
        } else {
            timeStr = hStr + ":" + m + ":" + s;
        }
        this.labTime.string = timeStr;
    },

    //刷新显示
    refreshShow: function refreshShow() {
        this.isSynchrTime = true;
        var currentTimeStamp = CL.MAINMANAGE.getServerTime();
        var leftTimeStamp = this.endTimeStamp - currentTimeStamp;
        if (leftTimeStamp > 0) {
            this.schedule(this.betUpdater, 1);
        } else {
            this.getCurrentIsuse();

            if (this._rewardListType == 0) {
                this.getIsuseList(this.lotteryId, "1", "1");
            } else {
                this.getIsuseList(this.lotteryId, "1", "11");
            }
        }
    },

    //得到上一期期数
    getLastIsuseNum: function getLastIsuseNum(cur, len) {
        if (this.isuseNum == 0 || cur == 0) return "?";
        var lastNum = cur - 1;
        var lastStr = lastNum.toString();
        var leftnum = this.isuseNum.substr(0, this.isuseNum.length - len);
        var rightnum = lastStr.length >= len ? lastStr : "0" + lastStr;
        return leftnum + rightnum;
    },

    //获取当期是否截止
    getCurrentIsuseAvailable: function getCurrentIsuseAvailable() {
        var currentTimeStamp = CL.MAINMANAGE.getServerTime();
        var leftTimeStamp = this.endTimeStamp - currentTimeStamp;
        if (leftTimeStamp <= 0) {
            return false;
        } else {
            return true;
        }
    },

    //获取当期信息
    getCurrentIsuse: function getCurrentIsuse() {
        this.isPre = false;
        this.scInterTime = 0;
        var recv = function (ret) {
            var obj = ret.Data;
            if (!Utils.isEffective(obj)) {
                this._hasCurisuse = false;
                cc.error("betcontrol currentisuse data null !");
                return;
            }

            if (ret.Code == 49) {
                this._hasCurisuse = false;
                cc.error("betcontrol currentisuse code=49 !");
                return;
            }

            if (ret.Code === 0) {
                // cc.log('当期信息',obj);
                this.isuseNum = obj.IsuseNum;
                var beginTimeStr = obj.BeginTime;
                var endTimeStr = obj.EndTime;
                var beginTimeDate = Utils.getDateForStringDate(obj.BeginTime.toString());
                var endTimeDate = Utils.getDateForStringDate(obj.EndTime.toString());
                this.beginTimeStamp = Date.parse(beginTimeDate) / 1000;
                this.endTimeStamp = Date.parse(endTimeDate) / 1000;
                cc.log('time 开始时间:' + this.beginTimeStamp + '结束时间:' + this.endTimeStamp);
                var titleLabelStr = "<color=#747474>距离</color><color=#3B85FF>" + this.isuseNum.toString() + "</color><color=#747474>期截止</color>";
                this.rtPeriods.string = titleLabelStr;
                this.schedule(this.betUpdater, 1);

                this._hasCurisuse = true;
            } else {
                if (ret.Code == 90 || ret.Code == 91) {
                    this.isPre = ret.Code == 90 ? true : false;

                    this.isuseNum = obj.IsuseNum;
                    var titleLabelStr = "<color=#747474>第</color><color=#3B85FF>" + this.isuseNum.toString() + "</color><color=#747474>期</color>";
                    this.rtPeriods.string = titleLabelStr;
                    this.labTime.string = ret.Msg;
                    this._hasCurisuse = true;
                } else {
                    this._hasCurisuse = false;
                }
            }
        }.bind(this);

        var data = {
            Token: User.getLoginToken(),
            LotteryCode: this.lotteryId
        };
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.GETRECURRENTISUSE, data, recv.bind(this), "POST", 5000);
    },

    //展开列表
    onShowIsuseList: function onShowIsuseList() {
        //if(this.labNullR.active == false)
        {
            this.rewardHistoryIssue.active = true;
            this.rewardHistoryIssue.emit('isuse-in');
            this.spHistoryArrow.node.rotation = 0;
        }
    },

    //收回列表
    onHideIsuseList: function onHideIsuseList() {
        this.rewardHistoryIssue.emit('isuse-out');
        this.spHistoryArrow.node.rotation = 180;
    },

    addEffectIsuseContent: function addEffectIsuseContent(data, curPre) {
        // cc.log('查看数据是否正确',data);
        this.labBet.string = '';
        var count = 0;
        for (var i = 0; i < data.length; i++) {
            var petNumberArray = [];
            if (data[i].Number != "") {
                petNumberArray = data[i].Number.split(" ");
                //cc.log('betcontrol 当前历史列表',petNumberArray);
            }

            if (i == 0) {
                var curIsuse = 0;
                if (this.isuseNum == undefined || this.isuseNum == "") {
                    this.isuseNum = 0;
                    curIsuse = 0;
                } else {
                    curIsuse = parseInt(this.isuseNum);
                }
                var newIsuse = parseInt(data[i].IsuseNum);
                var curInt = 0;
                curInt = Math.abs(curIsuse - newIsuse);
                if (curInt == 1) {
                    var isuse = '<color=#747474>' + '第 ' + '</color>' + '<color=#3B85FF>' + data[i].IsuseNum + '</color>' + '<color=#747474>' + ' 期' + '</color>';
                    this.curRewardInfo.getChildByName("rtPeriods").getComponent(cc.RichText).string = isuse;
                    this.labBet.string = data[i].Number == '' ? "等待开奖中..." : data[i].Number;
                    this.labBet.node.active = true;
                } else {
                    var isu = '?';
                    if (curIsuse != 0) {
                        isu = curIsuse - 1;
                    }
                    var isuse = '<color=#747474>' + '第 ' + '</color>' + '<color=#3B85FF>' + isu.toString() + '</color>' + '<color=#747474>' + ' 期' + '</color>';
                    this.curRewardInfo.getChildByName("rtPeriods").getComponent(cc.RichText).string = isuse;
                    this.labBet.string = "等待开奖中...";
                    this.labBet.node.active = true;
                }
            } else {

                if (data[i].Number != "") {
                    count++;
                }

                if (this.isuseContent.node.getChildByName('chat_11X5Reward')) {
                    this.isuseContent.node.removeAllChildren();
                }
                if (!this._createNode) {
                    var reward = cc.instantiate(curPre);
                    reward.getComponent('chat_28Reward').init({
                        isuse: data[i].IsuseNum,
                        num: data[i].Number
                    });
                    this.isuseContent.node.addChild(reward);
                } else {
                    this.isuseContent.node.children[i - 1].getComponent('chat_28Reward').init({
                        isuse: data[i].IsuseNum,
                        num: data[i].Number
                    });
                }
            }
        }

        if (count == 0) {
            this._rewardListType = 2;
        } else if (count + 1 == data.length) {
            this._rewardListType = 0;
        } else {
            this._rewardListType = 1;
        }
        this.isWaitOpen = true;
        this._createNode = true;
    },

    //添加和刷新开奖列表
    addIsuseContent: function addIsuseContent(data, curPre) {
        // cc.log('查看数据是否正确3',data);
        var count = 0;
        for (var i = 0; i < data.length; i++) {
            var spriteNums = [];
            var petNumberArray = [];
            if (data[i].Number != "") {
                petNumberArray = data[i].Number.split(" ");
                for (var j = 0; j < petNumberArray.length; j++) {
                    spriteNums.push(petNumberArray[j]);
                }
            }

            var lens = 3;
            var isuseStr = data[i].IsuseNum;
            var isuseTemp = isuseStr.substr(isuseStr.length - lens);
            var isuseInt = parseInt(isuseTemp);
            var curInt = 0;
            if (this.isuseNum != 0) {
                var curStr = this.isuseNum;
                var curTemp = curStr.substr(curStr.length - lens);
                curInt = parseInt(curTemp);
            }

            if (i == 0) {
                var diceNode = this.curRewardInfo.getChildByName("LuckyBallContent");
                if (curInt - 1 == isuseInt || this.isPre) //当前期号与列表期号相差1
                    {
                        var isuse = '<color=#747474>' + '第 ' + '</color>' + '<color=#3B85FF>' + data[i].IsuseNum + '</color>' + '<color=#747474>' + ' 期' + '</color>';
                        this.curRewardInfo.getChildByName("rtPeriods").getComponent(cc.RichText).string = isuse;
                        if (data[i].LotteryCode == 301 || data[i].LotteryCode == 202) {

                            if (spriteNums.length > 0) {
                                for (var k = 0; k < spriteNums.length; k++) {
                                    var cout = k + 1;
                                    var ballname = "ball" + cout.toString();
                                    try {
                                        if (data[i].LotteryCode == 301) {
                                            diceNode.getChildByName(ballname).getChildByName("label").getComponent(cc.Label).string = spriteNums[k];
                                        } else if (data[i].LotteryCode == 202) {
                                            diceNode.getChildByName(ballname).getChildByName("label").getComponent(cc.Label).string = spriteNums[k] == "0" ? "?" : spriteNums[k];
                                        }
                                    } catch (error) {}
                                }
                            }
                            diceNode.active = true;
                        }
                    } else {
                    var lastIsuse = this.getLastIsuseNum(curInt, lens);
                    var laseIsuse = '<color=#747474>' + '第 ' + '</color>' + '<color=#3B85FF>' + lastIsuse + '</color>' + '<color=#747474>' + ' 期' + '</color>';
                    this.curRewardInfo.getChildByName("rtPeriods").getComponent(cc.RichText).string = laseIsuse;
                    diceNode.active = true;
                }
            } else {
                if (data[i].Number != "") {
                    count++;
                }

                if (this.isuseContent.node.getChildByName('chat_28Reward')) {
                    this.isuseContent.node.removeAllChildren();
                }
                if (!this._createNode) {
                    var reward = cc.instantiate(curPre);
                    reward.getComponent('chat_public_reward').init({
                        nums: spriteNums,
                        isuse: data[i].IsuseNum
                    });
                    this.isuseContent.node.addChild(reward);
                } else {
                    this.isuseContent.node.children[i - 1].getComponent('chat_public_reward').init({
                        nums: spriteNums,
                        isuse: data[i].IsuseNum
                    });
                }
            }
        }

        if (count == 0) {
            this._rewardListType = 2;
        } else if (count + 1 == data.length) {
            this._rewardListType = 0;
        } else {
            this._rewardListType = 1;
        }

        this.isWaitOpen = true;
        this._createNode = true;
    },

    //刷新开奖列表信息
    refreshOpen: function refreshOpen(data) {
        if (data.LotteryCode == this.lotteryId) {
            if (this.openList.length > 0) {
                this.openList.unshift(data);
                this.openList.pop();
            } else {
                this.openList.push(data);
            }
            this.resetIsuseList();
        }
    },

    resetIsuseList: function resetIsuseList() {
        //this.isuseContent.node.removeAllChildren();
        var ID = parseInt(lotteryid);
        switch (ID) {
            case 40010:
                //幸运28
                {
                    this.addEffectIsuseContent(this.openList, this.bet28Reward);
                }
                break;
            case 40020:
                //加拿大28
                {
                    this.addEffectIsuseContent(this.openList, this.bet28Reward);
                }
                break;
            case 301:
                //ssc
                {
                    this.addIsuseContent(this.openList, this.X5SSCReward);
                }
                break;
            case 202:
                //115
                {
                    this.addIsuseContent(this.openList, this.X5SSCReward);
                }
                break;
            default:
                break;
        }
        this.isUpdating = false;
    },

    //获取开奖列表
    getIsuseList: function getIsuseList(lotteryid, pageindex, pagesize) {
        if (this.isUpdating == true) return;
        this.isUpdating = true;
        var recv = function (ret) {
            if (ret.Code !== 0) {
                //this.labNullR.active = true;
                this.isUpdating = false;
            } else {
                var data = ret.Data;
                this.showIsuseList(data, lotteryid);
            }
        }.bind(this);

        var data = {
            Token: User.getLoginToken(),
            LotteryCode: lotteryid,
            PageNumber: pageindex,
            RowsPerPage: pagesize
        };
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.GETTHELOTTERYLIST, data, recv.bind(this), "POST");
    },

    showIsuseList: function showIsuseList(data, lotteryid) {
        // cc.log('查看列表信息',data,lotteryid);
        if (Utils.isEffective(data)) {
            this.labNullR.active = false;
            //this.isuseContent.node.removeAllChildren();
            this.openList = data.slice();

            var ID = parseInt(lotteryid);
            switch (ID) {
                case 40010:
                    //幸运28
                    {
                        this.addEffectIsuseContent(data, this.bet28Reward);
                    }
                    break;
                case 40020:
                    //加拿大28
                    {
                        this.addEffectIsuseContent(data, this.bet28Reward);
                    }
                    break;
                case 301:
                    //ssc
                    {
                        this.addIsuseContent(data, this.X5SSCReward);
                    }
                    break;
                case 202:
                    //115
                    {
                        this.addIsuseContent(data, this.X5SSCReward);
                    }
                    break;
                default:
                    break;
            }
        } else {
            cc.error("betcontrol getIsuseList data null");
            //this.labNullR.active = true;
            this.isUpdating = false;
        }
        this.isUpdating = false;
    },

    onDestroy: function onDestroy() {
        window.Notification.offType("rOpen_push");
    }
});

cc._RF.pop();